package cn.vcjf.weapp.common.web;

import cn.vcjf.weapp.common.TableAlias;
import cn.vcjf.weapp.common.util.ThreadContextHolder;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableLogic;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Date;

/**
 * @Auther: zhengzihang
 * @Date: 2018/7/5 18:20
 * @Description:
 */
public class PageModel<T> implements Serializable {

	private static final long serialVersionUID = 12L;
	@TableField(exist=false)
	private Integer page;
	@TableField(exist=false)
	private Integer limit;

	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(exist=false)
	protected Date begDate;

	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(exist=false)
	protected Date endDate;

	/**
	 * 创建人
	 */
	protected String createUser;

	/**
	 * 创建时间
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	protected Date createTime;
	/**
	 * 创建时间查询,开始
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(exist=false)
	protected Date createTimeBegDate;
	/**
	 * 创建时间查询,结束
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(exist=false)
	protected Date createTimeEndDate;
	/**
	 * 修改人
	 */
	protected String updateUser;

	/**
	 * 修改时间
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	protected Date updateTime;

	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(exist=false)
	protected Date updateTimeBegDate;

	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(exist=false)
	protected Date updateTimeEndDate;
	/**
	 * 删除标记
	 */
	@TableLogic
	protected Integer deleteFlag;

	protected Integer sysUserId;
	@JsonIgnore
	public Integer getPage() {
		if(page==null||page<=0){
			page=1;
		}
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}
	@JsonIgnore
	public Integer getLimit() {
		if (limit == null){
			limit=0;
		}
		if(limit<=0){
			limit=10;
		}
		return limit;
	}

	public PageModel() {
		sysUserId = ThreadContextHolder.getSysUserId();
		System.out.println(this.getClass().getName()+" ---> sysUserId = "+sysUserId);
	}

	/**
	 * 构造分页实体类
	 * @return  分页对象
	 */
	@JsonIgnore
	public Page<T> getPageInfo(){
		return new Page<T>(this.getPage(), this.getLimit());
	}
	@JsonIgnore
	public EntityWrapper<T> getQueryWrapper() {
		EntityWrapper<T> wrapper = new EntityWrapper<T>();
		Class clazz = this.getClass();
		Annotation tableAliasAnnotation = clazz.getAnnotation(TableAlias.class);

		String tableAlias = "";
		if (tableAliasAnnotation != null && StringUtils.isNotBlank(((TableAlias)tableAliasAnnotation).value())) {
//			fieldName = fileAnnotation.value() + "." + fieldName;
			tableAlias = ((TableAlias)tableAliasAnnotation).value();
		}
		boolean flag = true;
		Field[] declaredFields = clazz.getDeclaredFields();
		for (Field field : declaredFields) {
			boolean isStatic = Modifier.isStatic(field.getModifiers());
			if(!isStatic) {
				Annotation annotation = field.getAnnotation(TableField.class);
				if (annotation!=null){
					TableField tableField=(TableField)(annotation);
					if(!tableField.exist()){
						continue;
					}
				}

				String fieldName = field.getName();
				//根据字段前缀注解,自动为拼接的条件添加前缀,用做联表查询
				TableAlias fileAnnotation = field.getAnnotation(TableAlias.class);
				if (fileAnnotation != null && StringUtils.isNotBlank(fileAnnotation.value())) {
					fieldName = fileAnnotation.value() + "." + fieldName;
				}else if(StringUtils.isNotBlank(tableAlias)){
					fieldName = tableAlias + "." + fieldName;
				}
				if (fieldName.indexOf("sysUserId")!=-1){
					wrapper.eq(fieldName, this.getSysUserId());
					flag = false;
					continue;
				}
				Object value = null;
				try {
					field.setAccessible(true);
					value = field.get(this);
					if (null != value) {
						if (value instanceof String) {
							if (StringUtils.isNotBlank((String) value)) {
								wrapper.eq(fieldName, value);
							}
						} else if (value instanceof Integer){
							if (null != value) {
								wrapper.eq(fieldName, value);
							}
						}else if (value instanceof Date){
							try{
								if (null != value) {
									if (value.toString().toLowerCase().indexOf("beg")!=-1){
										wrapper.le(fieldName, value);
									}else if(value.toString().toLowerCase().indexOf("end")!=-1){
										wrapper.ge(fieldName, value);
									}
								}
							}catch (Exception e){

							}

						}
					}
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		if (flag && this.getSysUserId() != null) {
			wrapper.eq((StringUtils.isNotBlank(tableAlias) ? tableAlias + "." : "")
					+ "sysUserId", this.getSysUserId());
		}
		return wrapper;
	}
	public void setLimit(Integer limit) {
		this.limit = limit;
	}
	@JsonIgnore
	public Date getBegDate() {
		return begDate;
	}

	public void setBegDate(Date begDate) {
		this.begDate = begDate;
	}
	@JsonIgnore
	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	@JsonIgnore
	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}
	@JsonIgnore
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	@JsonIgnore
	public String getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}
	@JsonIgnore
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	@JsonIgnore
	public Integer getDeleteFlag() {
		return deleteFlag;
	}

	public void setDeleteFlag(Integer deleteFlag) {
		this.deleteFlag = deleteFlag;
	}
	@JsonIgnore
	public Date getCreateTimeBegDate() {
		return createTimeBegDate;
	}

	public void setCreateTimeBegDate(Date createTimeBegDate) {
		this.createTimeBegDate = createTimeBegDate;
	}
	@JsonIgnore
	public Date getCreateTimeEndDate() {
		return createTimeEndDate;
	}

	public void setCreateTimeEndDate(Date createTimeEndDate) {
		this.createTimeEndDate = createTimeEndDate;
	}
	@JsonIgnore
	public Date getUpdateTimeBegDate() {
		return updateTimeBegDate;
	}

	public void setUpdateTimeBegDate(Date updateTimeBegDate) {
		this.updateTimeBegDate = updateTimeBegDate;
	}
	@JsonIgnore
	public Date getUpdateTimeEndDate() {
		return updateTimeEndDate;
	}

	public void setUpdateTimeEndDate(Date updateTimeEndDate) {
		this.updateTimeEndDate = updateTimeEndDate;
	}
	@JsonIgnore
	public Integer getSysUserId() {
		return sysUserId;
	}

	public void setSysUserId(Integer sysUserId) {
		this.sysUserId = sysUserId;
	}

	public void preInsert() {
		this.setCreateUser("admin");
		this.setCreateTime(new Date());
	}

	public void preUpdate() {
		this.setUpdateUser("admin");
		this.setUpdateTime(new Date());
	}
}
